package com.itheima.mm.controller;
import com.itheima.mm.constants.Constants;
import com.itheima.mm.entity.PageResult;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.entity.Result;
import com.itheima.mm.pojo.Course;
import com.itheima.mm.pojo.User;
import com.itheima.mm.security.PreAuthorize;
import com.itheima.mm.service.ICourseService;
import com.itheima.mm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 包名:com.itheima.mm.controller
 * 作者:Leevi
 * 日期2020-01-15  11:22
 */
@RestController
@RequestMapping("course")
public class CourseController {
    @Autowired
    private ICourseService courseService;

    @RequestMapping("findAll")
    public Result findAll(int status) throws IOException {
        try {
            //2. 调用业务层的方法，查询所有
            List<Course> courseList = courseService.findAll(status);

            return new Result(true,"获取学科列表成功",courseList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取学科列表失败");
        }
    }

    @RequestMapping("delete")
    public Result delete(int id) throws IOException {
        try {
            //2. 调用业务层的方法，根据id删除学科信息
            courseService.deleteById(id);
            //删除成功
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false,e.getMessage());
        }
    }

    @RequestMapping("update")
    public Result update(@RequestBody Course course) throws IOException {
        try {
            //2. 调用业务层的方法，修改学科信息
            courseService.update(course);

            return new Result(true,"修改学科成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"修改学科失败");
        }
    }
    @RequestMapping("findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean) throws IOException {
        try {
            //2. 调用业务层的方法，进行分页查询
            PageResult pageResult = courseService.findByPage(queryPageBean);
            //查询成功
            return new Result(true,"获取学科列表成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取学科列表失败");
        }
    }

    @PreAuthorize("COURSE_ADD")//给该方法指定具体的权限
    @RequestMapping("add")
    public Result add(@RequestBody Course course, HttpSession session) throws IOException {
        try {
            //2. 设置其他数据:createDate、userId
            course.setCreateDate(DateUtils.parseDate2String(new Date()));//设置学科创建时间为当前时间
            User user = (User) session.getAttribute(Constants.MM_USER_USERNAME);
            course.setUserId(user.getId());//设置添加学科的用户的id为当前用户的id

            //3. 调用业务层的方法，保存学科信息
            courseService.add(course);

            //添加成功
           return new Result(true,"添加学科成功");
        } catch (Exception e) {
            e.printStackTrace();
            //添加失败
            return new Result(false,"添加学科失败");
        }
    }
}
